import tkinter as tk
import sys

class StdoutRedirector(object):
    def __init__(self, text_widget):
        self.text_space = text_widget

    def write(self, string):
        self.text_space.insert(tk.END, string)
        self.text_space.see(tk.END)
        # self.old_stdout.write(string)
        
    def flush(self):
        pass

    def __enter__(self):
        self.old_stdout = sys.stdout
        sys.stdout = self
        return self
    
    def __exit__(self, exc_type, exc_value, traceback):
        sys.stdout = self.old_stdout